const msgchat = {
	filepath: '',
	initwork: function(){
		var innerAudioContext = uni.createInnerAudioContext();
		uni.onSocketMessage(function (res) {	
		  console.log('服务器信息msgchat' + res.data);
		   var jsonmsg = JSON.parse(res.data);
		   if ((jsonmsg.emit == 'chatMessage' || jsonmsg.emit == 'addfriend') && uni.getStorageSync('yinstus') == 1) {
		   	innerAudioContext.autoplay = true;
		   	innerAudioContext.src = 'static/image/yin.mp3';
		   	innerAudioContext.play();
		   }
		   if (jsonmsg.emit == 'chatMessage'){
		   	// #ifdef APP-PLUS
			let msgListpa = 'msgList'+ jsonmsg.data.msg.userinfo.uid +'.text';
			plus.io.resolveLocalFileSystemURL('/storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/msgio/'+ msgListpa, function(entry){
					//删除文件
					// entry.remove( function ( entry ) {
					// 	console.log( "Remove succeeded" );
					// }, function ( e ) {
					// 	console.log( e.message );
					// } );
					// return false;
					entry.file(function(file) {
						var fileReader = new plus.io.FileReader();
						fileReader.readAsText(file, 'utf-8');
						fileReader.onloadend = function(evt) {
							var list = JSON.parse(evt.target.result) //本地消息
							msgchat.savafile(list, jsonmsg.data, jsonmsg.data.msg.userinfo.uid);
						}
					})
			}, function ( e ) {
				console.log( "Resolve file URL failed: " + e.message );
				msgchat.savafile(null, jsonmsg.data, jsonmsg.data.msg.userinfo.uid);
			} );
			// #endif	
		   }
		});
	},
	//lists 原数据
	//data  新增数据
	//uid   对象ID
	savafile: function(lists, data, uid){
		plus.io.requestFileSystem( plus.io.PRIVATE_DOC, function(fs){
				// fs.root是根目录操作对象DirectoryEntry
				var msgListpa = '../msgio/msgList' + uid + '.text';
				fs.root.getFile(msgListpa, {create:true}, function(fileEntry){
					var Path = fileEntry.toURL(); //获取当前文件的路径
					let msglist = [];
					if(lists){
						msglist = lists;
					}
					msglist.push(data);
					fileEntry.createWriter(function(data) {  
						//对文件进行写入操作
						data.write(JSON.stringify(msglist));
						console.log('写入成功')
					}, function(e) {
					})
				});
		} );
	},
	//lists 原数据
	//data  新增数据
	//uid   对象ID
	savahmfile: function(lists, data, uid){
		plus.io.requestFileSystem( plus.io.PRIVATE_DOC, function(fs){
				// fs.root是根目录操作对象DirectoryEntry
				var msgListpa = '../msgio/msgList' + uid + '.text';
				fs.root.getFile(msgListpa, {create:true}, function(fileEntry){
					var Path = fileEntry.toURL(); //获取当前文件的路径
					console.log(Path)
					let msglist = [];
					if(lists){
						msglist = lists;
					}
					if(msglist.length == 0){
						msglist.push(data);
					}
					console.log(msglist)
					fileEntry.createWriter(function(data) {  
						//对文件进行写入操作
						data.write(JSON.stringify(msglist));
						console.log('写入成功')
					}, function(e) {
					})
				});
		} );
	}
}
export default msgchat